﻿using System.Collections;
using System.Collections.Generic;
using IDAL;
using Model;

namespace BLL
{
    public class CategoryService
    {
        private static readonly ICategoryDAL dal = DALFactory.DataAccess.createCategoryDAL();

        //增加分类
        public Hashtable addCategory(
                                    int categoryOrder,
                                    string categoryName,
                                    string chineseName)
        {
            Hashtable ht = new Hashtable();
            Category category = getCategoryByOrder(categoryOrder);
            if (category != null)
            {
                ht.Add("result", "0");
                ht.Add("message", "分类序号不能重复");
                return ht;
            }
            category = getCategoryByName(categoryName);
            if (category != null)
            {
                ht.Add("result", "0");
                ht.Add("message", "英文名称已经存在");
                return ht;
            }

            category = getCategoryByChineseName(chineseName);
            if (category != null)
            {
                ht.Add("result", "0");
                ht.Add("message", "中文名称已经存在");
                return ht;
            }
            category = new Category()
            {
                categoryOrder = categoryOrder,
                categoryName = categoryName,
                chineseName = chineseName
            };
            dal.saveCategory(category);
            ht.Add("result", "1");
            ht.Add("message", "分类保存成功");
            return ht;
        }

        //删除分类
        public Hashtable delCategory(int id)
        {
            Hashtable ht = new Hashtable();
            Category category = getCategoryById(id);
            if (category == null)
            {
                ht.Add("result", "0");
                ht.Add("message", "分类不存在");
                return ht;
            }

            dal.delCategory(category);
            ht.Add("result", "1");
            ht.Add("message", "删除成功");
            return ht;
        }

        //更改分类名称
        public Hashtable changeCategory(int id, string categoryName, string chineseName)
        {
            Hashtable ht = new Hashtable();
            Category category = getCategoryById(id);
            if (category == null)
            {
                ht.Add("result", "0");
                ht.Add("message", "分类不存在");
                return ht;
            }
            category.categoryName = categoryName;
            category.chineseName = chineseName;
            dal.updateCategory(category);
            ht.Add("result", "1");
            ht.Add("message", "更新成功");
            return ht;
        }

        //更改分类序号
        public Hashtable changeOrder(int id, int categoryOrder)
        {
            Hashtable ht = new Hashtable();

            Category category = getCategoryByOrder(categoryOrder);
            if (category != null)
            {
                ht.Add("result", "0");
                ht.Add("message", "序号不能重复");
                return ht;
            }

            category = getCategoryById(id);
            if (category == null)
            {
                ht.Add("result", "0");
                ht.Add("message", "分类不存在");
                return ht;
            }

            category.categoryOrder = categoryOrder;
            dal.updateCategory(category);
            ht.Add("result", "1");
            ht.Add("message", "更新成功");
            return ht;
        }

        //根据ID得到分类
        public Category getCategoryById(int id)
        {
            return dal.getCategory(id);
        }

        //根据name得到分类
        public Category getCategoryByName(string categoryName)
        {
            return dal.getCategory(categoryName);
        }

        //根据chinaesename得到分类
        public Category getCategoryByChineseName(string chineseName)
        {
            return dal.getCategoryByChineseName(chineseName);
        }

        //根据order得到分类
        public Category getCategoryByOrder(int categoryOrder)
        {
            return dal.getCategoryByOrder(categoryOrder);
        }

        //得到分类列表
        public IList<Category> getCategoryList()
        {
            return dal.getCategory();
        }

    }
}
